home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / mflms101.arc / TESTINTS.C < prev    next >
C/C++ Source or Header  |  1989-11-25  |  4KB  |  122 lines

  1. /*
  2. **          TEST PROGRAM FOR INTERRUPT HANDLER FUNCTIONS
  3. **  This program demonstrates the installable interrupt
  4. **  service routines contained in the functions ticker, ctlbrk(),
  5. **  and in SMDLx.LIB.
  6. **  The ticker routine installs on
  7. **  interrupt 1CH, and chains after completion to whatever
  8. **  routine may have already been hung on that interrupt.
  9. **  Ticker is not a C function as such, but a stand-alone assembly
  10. **  language routine which is installed by installtick() and removed
  11. **  by removetick().
  12. **  The ctlbrk() routine is used to exit the program and cleanup
  13. **  the interrupt vector table upon exit.  It installs upon int 23H.
  14. **
  15. **  Copyright 1986, S.E. Margison
  16. **  modified  1989, R.B. Stout
  17. **
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <dos.h>
  22. #include <mflsys.h>
  23. #include <mfltime.h>
  24. #include <mflconio.h>
  25. #include <mflfiles.h>
  26.  
  27. /* first, we must declare our control-break handler routine.
  28. ** this routine could do almost anything, but we will just
  29. ** set a flag that the main program can test, and print a message.
  30. ** Note that this should be declared BEFORE main() routine!
  31. */
  32.  
  33. int killit;
  34.  
  35. handler()    /* handler cannot return a value, so declare void */
  36. {
  37.         char *p;
  38.  
  39.         killit = TRUE;
  40.         Bputs("Control-C received; exiting program\n");
  41.         return(0);       /* return 0 to continue program */
  42. }
  43.  
  44. int count;       /* this is the variable which ticker will manipulate */
  45. extern void installtick(), removetick();
  46.  
  47. main()
  48. {
  49.         int i;
  50.  
  51. /* BEFORE we change the interrupt vector table, we MUST
  52. ** install our control-C trap.  If not, it would be possible
  53. ** for a fast typist to control-C out of the program after
  54. ** we mess up the vector table and before we grab the control-C
  55. ** handler vector.   Result:  Well, just turn off the power switch
  56. ** and start all over again!!!
  57. */
  58.  
  59.         ctlbrk(&handler);       /* install our trap */
  60.         killit = FALSE;            /* and clear our trap flag */
  61.  
  62.  
  63. /* Use installtick() to pass the desired variable address to the ticker
  64. ** process and install it upon the interrupt.
  65. */
  66.  
  67.         installtick(&count);
  68.  
  69.  
  70. /* this loop demonstrates the use of the ticker function.  As long as
  71. ** the count variable is not 0, it will be decremented 18.21 times
  72. ** per second.  ticker tests the value for zero and if it is,
  73. ** does not decrement it again.  This makes it unnecessary to try to
  74. ** "trap" count exactly at zero before the next interrupt occurs.
  75. ** To use ticker, place a value in count (18 = 1 second more or less).
  76. ** Then, check it every so often to see if it is zero (or whatever
  77. ** your heart desires).
  78. */
  79.  
  80.         i = 0;
  81.         while(!killit)  /* abort when control-C trap sets this flag     */
  82.         {
  83.                 count = 18;             /* set a value of 1 second      */
  84.                 while(count != 0) ;     /* loop until timed out         */
  85.                 /* do something useful here */
  86.                 printf("This is loop #%d\n", i++);
  87.         }       /* and go again */
  88.  
  89. /* if we are here, then our control-C trap worked! */
  90.  
  91. /* now, we have to clean up after ourselves.  It is necessary
  92. ** to restore the original vector table contents.  Alternate
  93. ** option is to turn off the power!!
  94. */
  95.  
  96. /* first, remove the ticker from the interrupt table.
  97. ** WARNING:  FAILURE TO REMOVE TICKER BEFORE EXITING PROGRAM
  98. **           WILL DEFINITELY (as in positively!) CRASH THE
  99. **           SYSTEM ALMOST IMMEDIATELY!!!!!!
  100. */
  101.         removetick();
  102.  
  103. /* finally, let's restore the original control-C handler by uninstalling
  104. ** our own.  A NULL parameter does the job!
  105. ** Yes, DOS restores the original vector upon exit from our program, but
  106. ** this shows that we can also do that ourselves.
  107. */
  108.  
  109.         ctlbrk(0);
  110. }
  111.  
  112. /* If using ticker, the following ctlbrk handler would be sufficient
  113. ** if it was desired to abort the program cleanly when a control-break
  114. ** was pressed:
  115.  
  116. handler
  117. {
  118.         removetick();
  119.         return(1);
  120. }
  121. */
  122.